package com.bawei.park2211a.person

import android.app.Activity
import android.app.DatePickerDialog
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.DatePicker
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.bean.AddPersonEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.park2211a.R
import com.bawei.park2211a.databinding.ActivityAddPersonBinding
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bumptech.glide.Glide
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.*

@AndroidEntryPoint
@Route(path = "/app/person/AddPersonActivity")
class AddPersonActivity : BaseActivity<ActivityAddPersonBinding, AddPersonViewModel>() {
    var addPersonEntity = AddPersonEntity()
    override fun initView() {
        val departmentListS = arrayListOf("研发部", "技术部", "市场部", "安保部", "管理部")
        val departmentListI = arrayListOf(2, 3, 4, 6, 5)
        val occupationS = arrayListOf("Andorid高级研发师", "Andorid高级技术员", "人事部经理", "安保科长", "常务董事")
        var departmentAdapter: ArrayAdapter<String> =
            ArrayAdapter(this, android.R.layout.simple_list_item_1, departmentListS)
        binding.perspnDepartment.adapter = departmentAdapter
        binding.perspnDepartment.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                addPersonEntity.departmentId = departmentListI[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
            }

        }
        var occupationAdapter: ArrayAdapter<String> =
            ArrayAdapter(this, android.R.layout.simple_list_item_1, occupationS)
        binding.perspnOccupation.adapter = occupationAdapter
        binding.personSubmit.setOnClickListener {
            addPersonEntity.name = binding.etPersonName.text.toString()
            addPersonEntity.id = addPersonEntity.id + 1
            viewModel.addPerson(addPersonEntity)

        }
        binding.personSumbitdata.setOnClickListener {
            TimePickerBuilder(this) { date, _ ->
                var sdf = SimpleDateFormat("yyyy-MM-dd")
                binding.personData.text = sdf.format(date)
            }.build().show()


        }
        binding.personUodataIcon.setOnClickListener {
            PictureSelector.create(this, 200).selectPicture()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 200 && resultCode == Activity.RESULT_OK) {
            val parcelable =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            Glide.with(this).load(parcelable?.path).into(binding.personIcon)
        }
    }

    override fun initData() {
        viewModel.addPersonSuccess.observe(this) {
            ARouter.getInstance().build("/app/person/PersonActivity").navigation()
        }
    }
}